<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>28.整合</title>
	<style>
	</style>
	<script src="../utils/index.js"></script>
	<link rel="stylesheet" href="../utils/ui.css">
</head>
<body>
	<div id="ui">
		<div class="x"></div>
		<div class="y"></div>
		<div class="z"></div>
	</div>
	<canvas id="gl" style="background-image: linear-gradient(rgb(0, 204, 255 ), rgb(255, 255, 255));"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		uniform mat4 u_modelMatrix;
		uniform mat4 u_viewMatrix;
		uniform mat4 u_projectionMatrix;

		attribute vec4 a_position;
		attribute vec4 a_normal;

		varying vec3 v_normal;
		varying vec3 v_position;
		varying vec3 v_dotLight;
		void main(){
			gl_Position=u_projectionMatrix*u_viewMatrix*u_modelMatrix*a_position;

			
			v_normal=mat3(u_projectionMatrix*u_viewMatrix*u_modelMatrix) * a_normal.xyz;
			v_position=gl_Position.xyz;
			v_dotLight=vec3(100,100,100)-gl_Position.xyz;
			//gl_Position=u_modelMatrix*u_viewMatrix*u_projectionMatrix*a_position;
		}
	</script>
	<script type="x-shader/x-fragment" id="fragment">
		precision mediump float;
		varying vec3 v_normal;
		varying vec3 v_position;
		varying vec3 v_dotLight;
		void main(){
			vec3 normal = normalize(v_normal);
			float parallelLight=dot(normal, normalize(vec3(100,200,100)));

			vec3 surfaceToLightDirection=normalize(v_dotLight);
			float pointLight=dot(normal,surfaceToLightDirection);
			gl_FragColor =vec4(0.,1.,1.,1.0);
			
			gl_FragColor.rbg*=pointLight;
			gl_FragColor.rbg*=parallelLight;
			//gl_FragColor.rgb += specular;
		}
	</script>
	
	<script>
		 /** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("gl")
		const gl=canvas.getContext("webgl")
		canvas.width=1000
		canvas.height=1000
		//流程 1.创建着色器 编译 链接
		const vertex=document.getElementById("vertex").textContent
		const fragment=document.getElementById("fragment").textContent
		
		const vertexShader=gl.createShader(gl.VERTEX_SHADER)
		const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)
		gl.shaderSource(vertexShader,vertex)
		gl.shaderSource(fragmentShader,fragment)
		gl.compileShader(vertexShader)
		gl.compileShader(fragmentShader)
		const program=gl.createProgram()
		gl.attachShader(program,vertexShader)
		gl.attachShader(program,fragmentShader)
		gl.linkProgram(program)
		gl.useProgram(program)
		gl.viewport(0,0,1000,1000)

		/*
			模型矩阵 好像又是世界矩阵 以后再看看

			视图矩阵

			投影矩阵
		*/
		const m4=webglUtils.Mat4
		let modelMatrix=m4.create()
		var camera = [0, 0, 200];
		var target = [0, 0, 0];
		var up = [0, 1, 0];
		var cameraMatrix = m4.lookAt(camera, target, up);
		var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
		var zNear =0.01;
		var zFar = 4000;
		var fieldOfViewRadians = webglUtils.degreesToRadians(60);
		let projectionMatrix=m4.perspective(fieldOfViewRadians, aspect, zNear, zFar)

		const u_modelMatrix=gl.getUniformLocation(program,"u_modelMatrix"); 
		const u_viewMatrix=gl.getUniformLocation(program,"u_viewMatrix"); 
		const u_projectionMatrix=gl.getUniformLocation(program,"u_projectionMatrix"); 

		const a_position=gl.getAttribLocation(program,"a_position"); 
		const a_normal=gl.getAttribLocation(program,"a_normal"); 


		const cube=webglUtils.createCube(100)
		let positions =cube.position
		let matrix = m4.xRotation(Math.PI);
		matrix = m4.translate(matrix, -50, -75, -15);
		let positionBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
		gl.enableVertexAttribArray(a_position);
		gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
		gl.vertexAttribPointer(a_position, 3, gl.FLOAT, false, 0, 0);
		
		let normalBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
		gl.enableVertexAttribArray(a_normal);
		gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
		gl.vertexAttribPointer(a_normal, 3, gl.FLOAT, false, 0, 0);

		gl.enable(gl.DEPTH_TEST)
		var primitiveType = gl.TRIANGLES;
		var offset = 0;
		var count = 16 * 6;
		var deg = webglUtils.degreesToRadians(50);
		deg = webglUtils.degreesToRadians(0.1);
		// modelMatrix=m4.translateX(modelMatrix,100)//移动模型矩阵
		// cameraMatrix=m4.translateX(cameraMatrix,100)//移动摄像机矩阵
		// projectionMatrix=m4.translateX(projectionMatrix,100)//移动投影矩阵

		let viewMatrix=m4.inverse(cameraMatrix)
		gl.uniformMatrix4fv(u_modelMatrix,false,modelMatrix)
		gl.uniformMatrix4fv(u_viewMatrix,false,viewMatrix)
		gl.uniformMatrix4fv(u_projectionMatrix,false,projectionMatrix)
		setInterval(()=>{
			modelMatrix=m4.rotateY(modelMatrix,deg)
			gl.uniformMatrix4fv(u_modelMatrix,false,modelMatrix)
			gl.drawArrays(primitiveType, offset, count);
		})
	</script>
</body>
</html>
